import Base64 from "@/libs/js-base64/index.js"
import Crypto from "@/utils/crypto2.js"
import LocalStorage from "@/utils/storage"
import {
	showToast
} from "@/utils/frameApi"
import request from "./request"
const BASE_UPLOAD = "https://global-application-001.downline.cn"



const getFileName2 = (tempFile, ossConfig) => {
  let key = "";
  let ossUrl = ""
  let name = tempFile.split('.');
  let leng = name.length;
  let nametext = name[leng - 2]
  if (nametext.split('//')[1]) {
    nametext = nametext.split('//')[1];
  }
  if (name.length === 2) {
    if (ossConfig.app_main_url) {
      ossUrl = ossConfig.oss_host_url + '/' + ossConfig.app_main_url + '/goods_image/' + nametext + '.' + name[1];
      key = ossConfig.app_main_url + '\/' + 'goods_image' + '\/' + nametext + '.' + name[1];
    } else {
      ossUrl = ossConfig.oss_host_url + '/goods_image/' + nametext + '.' + name[1];
      key = 'goods_image' + '\/' + nametext + '.' + name[1];
    }
  } else {
    if (ossConfig.app_main_url) {
      ossUrl = ossConfig.oss_host_url + '/' + ossConfig.app_main_url + '/goods_image/' + name[1] + '.' + name[2];
      key = ossConfig.app_main_url + '\/' + 'goods_image' + '\/' + name[1] + '.' + name[2];
    } else {
      ossUrl = ossConfig.oss_host_url + '/goods_image/' + name[1] + '.' + name[2];
      key = 'goods_image' + '\/' + name[1] + '.' + name[2];
    }
  }
  return {
    key,
    ossUrl
  }
}
/**
 * 
 */
export const uploadAPI = async (file) => {
	if (!file) {
		showToast({
			title: '未选择文件'
		})
		return Promise.reject({
			status: 500,
			msg: "未选择文件"
		})
	}
	let ossConfig = await request.post({
		url: "/mgr_public_platform_global",
		data: {
			invoke_method: 'public_page_4m_load_oss_conf'
		}
	}, true).catch((err) => {
		showToast({
			title: '系统异常，请稍后重试'
		})
		return Promise.reject({
			status: 500,
			msg: "系统异常，请稍后重试"
		});
	})
	if (ossConfig) {
		return new Promise((resolve, reject) => {
			var datasignature = ossConfig
			var policyText = {
				"expiration": "2222-01-01T12:00:00.000Z",
				"conditions": [
					["content-length-range", 0, 10485760]
				]
			};
			var policyBase64 = Base64.encode(JSON.stringify(policyText))
			var bytes =Crypto. Crypto.HMAC(Crypto.Crypto.SHA1, policyBase64, ossConfig.oss_accesskey, {
				asBytes: true
			});
			var signature = Crypto.Crypto.util.bytesToBase64(bytes);
			datasignature.policy = policyBase64;
			datasignature.signature = signature;
			datasignature.expire = Date.now() + 600000;
			let fileInfo = getFileName2(file, datasignature);
			let key = fileInfo.key;
			uni.uploadFile({
				url: datasignature.oss_host_url, //仅为示例，非真实的接口地址
				filePath: file,
				name: 'file',
				formData: {
					'key': key,
					'policy': datasignature.policy,
					'OSSAccessKeyId': datasignature.oss_accessid,
					'success_action_status': '200', //让服务端返回200,不然，默认会返回204
					'signature': datasignature.signature,
				},
				success(res) {
					resolve(fileInfo.ossUrl)
				},
				fail: () => {
					uni.showToast({
						title: '上传文件失败',
						icon: "non"
					})
					return Promise.reject({
						status: 500,
						msg: "上传失败"
					})
				}
			})
		})
	}
}